<?php
include_once '../../Config/config.php';
include_once '../../Config/conexion.php';
include_once '../../Model/Revisor.php';
include_once '../../Model/Paper.php';
include_once '../../Model/Horario.php';
include_once '../../Model/Sala.php';
error_reporting(E_ALL);

/** PHPExcel_IOFactory */
require_once '../../Plugin/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);



$i = 4;

$listaHorario = Horario::Listar();
$listaTrack = Track::Listar();
$listaSala = Sala::Listar();

foreach($listaHorario as $rowHorario) {


		//
		//Listar($track_id = '', $idioma_id = '', $estado = '', $anio='', $autor = '', $revisor = '', $IdPaper = '',$titulo = '',$aceptarpaper = '', $tipopaper = '', $publicado = '', $idhorario = '', $idsala = '')
		//
		$listaPaperTemp = Paper::Listar('','','','','','','','','','','',$rowHorario->IdHorario,'');
			
		if(count($listaPaperTemp) == 0){
			continue;
		}

	$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $rowHorario->dia.' '.$rowHorario->Hora);	
	$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setSize(20);
	$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setBold(true);
	$i = $i+2;	

foreach($listaSala as $rowSala) {

foreach($listaTrack as $rowTrack) {

	$listaPaperTemp = Paper::Listar($rowTrack->Id,'','','','','','','','','','',$rowHorario->IdHorario,$rowSala->IdSala);
	/*$sqlPaper = "select distinct *, pp.id_paper, tr.nombre as nombretrack, i.nombre as nombreidioma from paper pp left join track tr on tr.id_track = pp.id_track
		left join idioma i on i.id_idioma = pp.id_idioma
		left join sala s on pp.id_sala = s.id_sala
		where pp.id_track = ".$rowTrack["id_track"]."
		and pp.id_sala = ".$rowSala["id_sala"]."
		and pp.id_horario = ".$rowHorario["id"]."
		and pp.id_paper in (select id_paper from autor_paper where id_pago = 1)
		order by pp.id_paper";	
	$resPaper = $dbh->query($sqlPaper);*/

	
	//Cargando los autores
	$se_muestra = false;
	$listaPaper = array();
	$pagantes = "1"; //Solo pagantes;
	foreach($listaPaperTemp as $key => $value){
		if(count($value->Autores) <= 0){
			continue;
		}
		
		$se_muestra = false;
		foreach($value->Autores as $autorTemp){
			$autorTemp->CargarTransaccionExitosa();
			
			if($pagantes != ""){
				if(!is_null($autorTemp->Transaccion)){
					
					$tempPagante = $autorTemp->Transaccion->PagadoConfirmado == "1"?"1":"0";
					if($tempPagante == $pagantes){
						$se_muestra = true;
					}
				}
			}else{
				$se_muestra = true;
				}						
		}
		
		if(!$se_muestra){
			 continue;
		}
		$listaPaper[] = $value;
	}
	
	if(count($listaPaper) == 0){	
		continue;
	}	
	

	$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, "Track:");
	$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $rowTrack->Nombre);
	$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getFont()->setBold(true);
	
	$objPHPExcel->getActiveSheet()->setCellValue('G'.$i, $rowHorario->dia);
	$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->setCellValue('H'.$i, $rowHorario->Hora);
	
	$i++;
	$objPHPExcel->getActiveSheet()->setCellValue('G'.$i, "Location: ");
	$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->setCellValue('H'.$i, $rowSala->Nombre);
	
	$i++;
	
	$i = $i+2;

	foreach($listaPaper as $rowPaper) {
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$rowPaper->IdPaper);
		$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->setBold(true);
		
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $rowPaper->Titulo);				
		$objPHPExcel->getActiveSheet()->mergeCells('B'.$i.':I'.($i+2));
		$objPHPExcel->getActiveSheet()->getStyle('B'.$i.':I'.($i+2))->getAlignment()->setWrapText(true);
		$objPHPExcel->getActiveSheet()->getStyle('B'.$i.':I'.($i+2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
		
	
		
		foreach($rowPaper->Autores as $autorTemp){
			$autorTemp->CargarTransaccionExitosa();
		}
		
		
		$i = $i+3;
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, "Autores");
		$i++;
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, "Number");
		$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, "Name");
		$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$i, "University");
		$objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getFont()->setBold(true);
		
		$item = 1;
		foreach($rowPaper->Autores as $rowAutores) {
			$i++;
			if(!is_null($rowAutores->Transaccion) && $rowAutores->Transaccion->PagadoConfirmado === "1"){
				$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->applyFromArray(
		array(
			'font'    => array(
				'bold'      => true
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
			),
			'borders' => array(
				'top'     => array(
 					'style' => PHPExcel_Style_Border::BORDER_THIN
 				)
			),
			'fill' => array(
	 			'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
	  			'rotation'   => 90,
	 			'startcolor' => array(
	 				'argb' => 'FFFFED00'
	 			),
	 			'endcolor'   => array(
	 				'argb' => 'FFFFFFFF'
	 			)
	 		)
		)
);
				}
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $item++);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $rowAutores->Nombres." ".$rowAutores->Paterno." ".$rowAutores->Materno);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $rowAutores->Institucion->Nombre);			
		}
		
		$i=$i+2;
	}
	
	$i = $i+3;
}
}
}

// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Reporte.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;


?>